<template>
	<view class="wrap">
		<view class="mar-top-20 input-group bg-ff">
			<view class="input-row border-bottom-1px" v-if="salesmanData.auditStatus != undefined && salesmanData.auditStatus != null">
				<text class="title">业务员审核状态</text>
				<view v-if="salesmanData.auditStatus == 1" class="fc-52c mar-right-20">审核通过</view>
				<view v-if="salesmanData.auditStatus == 2" class="fc-df5 mar-right-20">审核失败</view>
				<view v-if="salesmanData.auditStatus == 3" class="fc-df5 mar-right-20">已停用</view>
				<view v-if="salesmanData.auditStatus == 0" class="fc-f1a mar-right-20">待审核</view>
			</view>
			<view class="input-column">
				<text class="title">姓名</text>
				<view class="input-item">
					<input class="padding-10" v-model="salesmanData.name" placeholder="请输入姓名" />
				</view>
			</view>
			<view class="input-column">
				<text class="title">手机号</text>
				<view class="input-item">
					<input class="padding-10" v-model="salesmanData.phone" placeholder="请输入手机号" />
				</view>
			</view>
			<view class="input-column">
				<text class="title">区域</text>
				<view class="input-item">
					<input class="padding-10" v-model="salesmanData.regionCode" placeholder="请输入地址" />
				</view>
				<!-- <view class="input-item">
					<pickerAddress class="fc-66" @change="changeAddres">{{salesmanData.city}}</pickerAddress>
				</view> -->
			</view>
			<view class="input-column">
				<text class="title">详细地址</text>
				<view class="input-item">
					<input class="padding-10" v-model="salesmanData.address" placeholder="请输入详细地址" />
				</view>
				<!-- <view class="input-item">
					<pickerAddress class="fc-66" @change="changeAddres">{{salesmanData.city}}</pickerAddress>
				</view> -->
			</view>
			<view class="input-column" v-if="salesmanData.auditStatus == 1">
				<text class="title">抽佣比例(%)</text>
				<view class="input-item">
					<input class="padding-10" disabled v-model="salesmanData.proportion"/>
				</view>
			</view>
			<view class="input-column">
				<text class="title fc-33 pad-top-20 dis-block" style="width: 40%;">身份证</text>
				<view class="flex" style="justify-content: space-around;">
					<view class="head-img mar-top-15" @click="uploadImgs(1)">
						<view v-if="!salesmanData.idFrontImg" class="yyzz fs-28">正面</view>
						<image v-else :src="salesmanData.idFrontImg" mode="widthFix"></image>
					</view>
					<view class="head-img mar-top-15 mar-left-25" @click="uploadImgs(2)">
						<view v-if="!salesmanData.idReverseImg" class="yyzz fs-28">反面</view>
						<image v-else :src="salesmanData.idReverseImg" mode="widthFix"></image>
					</view>
				</view>
			</view>
			<view class="btn-row row">
				<!--  -->
				<button v-if="salesmanData.auditStatus ==2" type="primary" @click="register(2)"
					class="btn-save width-90 fc-ff">重新提交</button>
				<view
					v-else-if="salesmanData.auditStatus == 0 || salesmanData.auditStatus == 1 || salesmanData.auditStatus == 3">
				</view>
				<button type="primary" v-else class="btn-save width-90" @tap="register(1)">提交申请</button>
			</view>
			<view class="flex" style="justify-content: center;padding-bottom: 50rpx;">
				<radio-group @click="radioGroup">
					<radio color="#84C0F4" :value="1" :disabled="disabledFlag" :checked="checkedIndex"
						style="transform:scale(0.72);"></radio>
				</radio-group>
				<view class="agreement2">
					登录代表您已同意<span class="tips">《用户协议》</span>、<span class="tips">《隐私政策》</span>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import uploadImage from '@/common/ossutil/uploadFile';
	export default {
		computed: {
			...mapState(['userInfo']),
		},
		data() {
			return {
				salesmanData: {
					name: '',
					phone: '',
					province: '广东省',
					city: '佛山市',
					area: '禅城区',
					regionCode: '',
					address: '',
					idFrontImg: '',
					idReverseImg: '',
				},
				checkedIndex: false,
				disabledFlag: false,
				tmpImageUrl: ''
			};
		},
		onLoad() {
			this.getUserSalesmanInfo()
		},
		methods: {
			// 是否需要获取业务人员审核信息
			// 1.进入使用wxUserId 查询用户是否正在审核中或审核状态把
			getUserSalesmanInfo() {
				if(!this.userInfo || !this.userInfo.id) {
					this.$store.dispatch('reLogin')
					return;
				}
				this.$apis.getSalesmanByWxUserId(this.userInfo.id).then(res => {
					if (res.data) {
						this.checkedIndex = true;
						this.salesmanData = res.data;
						// 判断审核状态 审核中不可编辑
						this.disabledFlag = res.data.auditStatus != 2 ? true : false;
					}
				})
			},
			// 提交申请
			async register(type) {
				if (!this.checkRoule()) return;
				// 过了校验之后那wxUserId
				this.salesmanData.wxUserId = this.userInfo.id
				// 判断是否重新审核 状态改为 待审核
				if (type == 2) {
					this.salesmanData.auditStatus = 0
					// 调用更新接口 
					let res = await this.$apis.eidSalesman(this.salesmanData);
					if (res) {
						uni.showToast({
							title: '提交成功'
						})
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							})
						}, 1200)
					} else {
						uni.showToast({
							title: '提交失败'
						})
					}
				} else {
					let res = await this.$apis.addSalesman(this.salesmanData);
					if (res) {
						uni.showToast({
							title: '提交成功'
						})
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							})
						}, 1200)
					} else {
						uni.showToast({
							title: '提交失败'
						})
					}
				}
			},
			// 选择身份证照片
			uploadImgs(type) {
				if (this.disabledFlag) return;
				var that = this
				uni.chooseImage({
					success(res) {
						let tempFilePaths = res.tempFilePaths;
						that.tmpImageUrl = tempFilePaths[0];
						that.uploadFile(type);
					}
				});
			},
			// 上传图片
			uploadFile(type) {
				uni.showLoading({
					title: '图片上传中'
				});
				uploadImage(0, this.tmpImageUrl, 'person/salesmanauth/', result => {
					if (type == 1) {
						this.salesmanData.idFrontImg = result
					} else if (type == 2) {
						this.salesmanData.idReverseImg = result
					}
					uni.hideLoading();
				});
			},
			// 协议勾选
			radioGroup() {
				this.checkedIndex = !this.checkedIndex
			},
			// 校验
			checkRoule() {
				var reg = /^1[0-9]{10,10}$/;
				if (!this.salesmanData.name) {
					uni.showToast({
						icon: 'none',
						title: '请输入姓名'
					});
					return false;
				}
				if (!this.salesmanData.phone) {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号'
					});
					return false;
				}
				if (!reg.test(this.salesmanData.phone)) {
					uni.showToast({
						icon: 'none',
						title: '手机号错误'
					});
					return false;
				}
				if (!this.salesmanData.regionCode) {
					uni.showToast({
						icon: 'none',
						title: '请选择所属区域'
					});
					return false;
				}
				if (!this.salesmanData.address) {
					uni.showToast({
						icon: 'none',
						title: '请输入详细地址'
					});
					return false;
				}
				if (!this.salesmanData.idFrontImg) {
					uni.showToast({
						icon: 'none',
						title: '请上传身份证正面'
					});
					return false;
				}
				if (!this.salesmanData.idReverseImg) {
					uni.showToast({
						icon: 'none',
						title: '请上传身份证反面'
					});
					return false;
				}
				if (!this.checkedIndex) {
					uni.showToast({
						icon: 'none',
						title: '请阅读并勾选协议'
					});
					return false;
				}
				return true;
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f6f6f6;

		.input-group {
			border-radius: 8rpx;

			.border-bottom-1px {
				position: relative;

				&::after {
					position: absolute;
					z-index: 1;
					right: 0;
					bottom: 0;
					left: 20upx;
					height: 1upx;
					content: '';
					-webkit-transform: scaleY(0.5);
					transform: scaleY(0.5);
					background-color: #e2e2e2;
				}
			}

			.input-row {
				display: flex;
				flex-direction: row;
				padding: 0 0 0rpx 20rpx;
				position: relative;
				background: #fff;
				justify-content: space-between;
				align-items: center;
				box-sizing: border-box;

				.title {
					width: 30%;
					padding: 20upx 0;
					font-weight: 700;
					line-height: 50upx;
					color: #333;
				}

				.input-row-item {
					width: 568rpx;

					input {
						text-align: left;
						font-size: 28rpx;
					}
				}
			}

			.input-column {
				padding: 30rpx 20rpx 10rpx 20rpx;
				// border-bottom: 1px solid #EEEEEE;

				.title {
					margin-left: 10rpx;
					font-size: 28rpx;
				}

				.input-item {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;

					input {
						width: 90%;
						color: #999;
					}
				}
			}
		}

		.head-img {
			width: 222rpx;
			height: 232rpx;
			border-radius: 8rpx;
			display: flex;
			align-items: center;
			overflow: hidden;
			border: 1px solid #F8F8F8;

			.yyzz {
				width: 222rpx;
				height: 232rpx;
				display: block;
				background: #F8F8F8;
				border-radius: 8rpx;
				text-align: center;
				line-height: 232rpx;
				margin: 20rpx 0;
				color: #000000;
			}

			image {
				width: 222rpx;
				height: 232rpx;
				display: block;
			}
		}

		.agreement2 {
			position: relative;
			top: 4px;

			.tips {
				color: #2C8CF0;
			}
		}

		.btn-row {
			width: 100%;
			box-sizing: border-box;
			margin-top: 60rpx;

			.btn-save {
				background-color: #84C0F4;
				height: 90upx;
				line-height: 90upx;
				font-size: 34rpx;
			}

			.width-55 {
				width: 55%;
			}

			.width-90 {
				width: 90%;
				margin: 34rpx auto;
			}
		}
	}
</style>
